using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace NGNntpPlugIn
{
	/// <summary>
	/// Summary description for ServerConfiguration.
	/// </summary>
	public class ServerConfiguration : System.Windows.Forms.Form
	{
		private NntpConfig m_config;

		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox tbUsername;
		private System.Windows.Forms.TextBox tbPassword;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox cbServers;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.CheckedListBox clbNewsgroups;
		private System.Windows.Forms.Button btnApply;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox tbServerName;
		private System.Windows.Forms.Button btnDelete;
		private System.Windows.Forms.CheckBox cbAuthenticate;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public ServerConfiguration(NntpConfig config)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			m_config = config;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.tbPassword = new System.Windows.Forms.TextBox();
			this.tbUsername = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.cbAuthenticate = new System.Windows.Forms.CheckBox();
			this.label3 = new System.Windows.Forms.Label();
			this.cbServers = new System.Windows.Forms.ComboBox();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.clbNewsgroups = new System.Windows.Forms.CheckedListBox();
			this.btnApply = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.tbServerName = new System.Windows.Forms.TextBox();
			this.btnDelete = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.SuspendLayout();
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.tbPassword);
			this.groupBox1.Controls.Add(this.tbUsername);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.cbAuthenticate);
			this.groupBox1.Location = new System.Drawing.Point(8, 80);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(320, 104);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Authentication:";
			// 
			// tbPassword
			// 
			this.tbPassword.Location = new System.Drawing.Point(80, 72);
			this.tbPassword.Name = "tbPassword";
			this.tbPassword.PasswordChar = '*';
			this.tbPassword.Size = new System.Drawing.Size(232, 20);
			this.tbPassword.TabIndex = 4;
			this.tbPassword.Text = "";
			// 
			// tbUsername
			// 
			this.tbUsername.Location = new System.Drawing.Point(80, 40);
			this.tbUsername.Name = "tbUsername";
			this.tbUsername.Size = new System.Drawing.Size(232, 20);
			this.tbUsername.TabIndex = 3;
			this.tbUsername.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 72);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "Password:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 40);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 23);
			this.label1.TabIndex = 1;
			this.label1.Text = "Username:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// cbAuthenticate
			// 
			this.cbAuthenticate.Location = new System.Drawing.Point(8, 16);
			this.cbAuthenticate.Name = "cbAuthenticate";
			this.cbAuthenticate.Size = new System.Drawing.Size(304, 24);
			this.cbAuthenticate.TabIndex = 0;
			this.cbAuthenticate.Text = "Authenticate to News Server";
			this.cbAuthenticate.CheckedChanged += new System.EventHandler(this.cbAuthenticate_CheckedChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(48, 23);
			this.label3.TabIndex = 1;
			this.label3.Text = "Servers:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// cbServers
			// 
			this.cbServers.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cbServers.Location = new System.Drawing.Point(56, 16);
			this.cbServers.Name = "cbServers";
			this.cbServers.Size = new System.Drawing.Size(272, 21);
			this.cbServers.TabIndex = 2;
			this.cbServers.SelectedIndexChanged += new System.EventHandler(this.cbServers_SelectedIndexChanged);
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.clbNewsgroups);
			this.groupBox2.Location = new System.Drawing.Point(8, 192);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(320, 168);
			this.groupBox2.TabIndex = 3;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "Newsgroups:";
			// 
			// clbNewsgroups
			// 
			this.clbNewsgroups.CheckOnClick = true;
			this.clbNewsgroups.Location = new System.Drawing.Point(8, 16);
			this.clbNewsgroups.Name = "clbNewsgroups";
			this.clbNewsgroups.Size = new System.Drawing.Size(304, 139);
			this.clbNewsgroups.TabIndex = 0;
			// 
			// btnApply
			// 
			this.btnApply.Location = new System.Drawing.Point(96, 368);
			this.btnApply.Name = "btnApply";
			this.btnApply.TabIndex = 4;
			this.btnApply.Text = "Apply";
			this.btnApply.Click += new System.EventHandler(this.btnApply_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(256, 368);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 5;
			this.btnCancel.Text = "Close";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(0, 48);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 23);
			this.label4.TabIndex = 5;
			this.label4.Text = "Address:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// tbServerName
			// 
			this.tbServerName.Location = new System.Drawing.Point(56, 48);
			this.tbServerName.Name = "tbServerName";
			this.tbServerName.Size = new System.Drawing.Size(224, 20);
			this.tbServerName.TabIndex = 6;
			this.tbServerName.Text = "";
			// 
			// btnDelete
			// 
			this.btnDelete.Location = new System.Drawing.Point(176, 368);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.TabIndex = 7;
			this.btnDelete.Text = "Delete";
			this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(288, 48);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(40, 23);
			this.button1.TabIndex = 8;
			this.button1.Text = "Find";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// ServerConfiguration
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(338, 400);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.btnDelete);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnApply);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.cbServers);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.tbServerName);
			this.Controls.Add(this.label4);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ServerConfiguration";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Server Configuration";
			this.Load += new System.EventHandler(this.ServerConfiguration_Load);
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void SetupForm()
		{
			if(cbServers.SelectedItem != null)
			{
				// load the selected server
				if(cbServers.SelectedItem is NntpServer)
				{
					// this is a server
					NntpServer server = (NntpServer) cbServers.SelectedItem;

					tbServerName.Text = server.Server;
					cbAuthenticate.Checked = server.Authenticate;
					if(server.Authenticate)
					{
						tbUsername.Text = server.UserName;
						tbPassword.Text = server.Password;
					}

					// get all the newsgroups for this server
					clbNewsgroups.Items.Clear();
					clbNewsgroups.Enabled = true;

					Nntp nntp = new Nntp();
					Cursor = Cursors.WaitCursor;
					try
					{
						if(!server.Authenticate)
							nntp.Connect(server.Server);
						else
							nntp.Connect(server.Server, server.Username, server.Password);

						foreach(object newsgroup in nntp.GetNewsgroups())
						{
							clbNewsgroups.Items.Add(newsgroup.ToString());
						}

						if(cbServers.SelectedItem is NntpServer)
						{
							// select items
							foreach(NntpNewsgroup ng in ((NntpServer) cbServers.SelectedItem).Newsgroups)
							{
								int index = clbNewsgroups.Items.IndexOf(ng.Name);
								if(index >= 0)
									clbNewsgroups.SetItemChecked(index, true);
							}
						}

						if(clbNewsgroups.Items.Count == 0)
						{
							clbNewsgroups.Items.Add("No Newsgroups Found");
							clbNewsgroups.Enabled = false;
						}

						nntp.Disconnect();
					}
					catch
					{
						// show error message
						clbNewsgroups.Items.Add("Error Connecting to Server");
						clbNewsgroups.Enabled = false;
					}
					Cursor = Cursors.Default;
					btnDelete.Enabled = true;
				}
				else
				{
					// this is the new item
					tbServerName.Text = "";
					tbUsername.Text = "";
					tbPassword.Text = "";
					clbNewsgroups.Items.Clear();

					clbNewsgroups.Enabled = true;
				}

				tbServerName.Enabled = true;
				tbUsername.Enabled = cbAuthenticate.Checked;
				tbPassword.Enabled = cbAuthenticate.Checked;
				btnApply.Enabled = true;
			}
			else
			{
				// disable the appropriate controls
				tbServerName.Enabled = false;
				tbUsername.Enabled = false;
				tbPassword.Enabled = false;
				clbNewsgroups.Enabled = false;
				btnApply.Enabled = false;
				btnDelete.Enabled = false;

				// set the values
				cbAuthenticate.Checked = false;
				tbServerName.Text = "";
				tbUsername.Text = "";
				tbPassword.Text = "";
				clbNewsgroups.Items.Clear();
			}
		}

		private void LoadServers()
		{
			// clear the servers
			cbServers.Items.Clear();

			// load the servers
			foreach(NntpServer server in m_config.Servers)
			{
				cbServers.Items.Add(server);
			}

			cbServers.Items.Insert(0, "------ New Server ------");

			SetupForm();
		}

		private void ServerConfiguration_Load(object sender, System.EventArgs e)
		{
			LoadServers();
		}

		private void cbServers_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SetupForm();
		}

		private void cbAuthenticate_CheckedChanged(object sender, System.EventArgs e)
		{
			tbUsername.Enabled = cbAuthenticate.Checked;
			tbPassword.Enabled = cbAuthenticate.Checked;
		}

		private void btnApply_Click(object sender, System.EventArgs e)
		{
			NntpServer server = null;

			if(cbServers.SelectedItem is NntpServer)
			{
				int index = m_config.Servers.IndexOf((NntpServer) cbServers.SelectedItem);

				if(index >= 0)
				{
					server = (NntpServer) m_config.Servers[index];
					if(server != null)
					{
						server.Server = tbServerName.Text;
						server.Authenticate = cbAuthenticate.Checked;
						server.Username = tbUsername.Text;
						server.Password = tbPassword.Text;
					}
				}
			}
			else
			{
				// create a new server
				server = new NntpServer(Guid.NewGuid(), tbServerName.Text, cbAuthenticate.Checked, tbUsername.Text, tbPassword.Text);

				// add this server to the collection
				m_config.Servers.Add(server);
			}

			if(server != null)
			{
				// save the newsgroups
				server.Newsgroups.Clear();

				foreach(object item in clbNewsgroups.CheckedItems)
				{
					// add this newsgroup
					server.Newsgroups.Add(new NntpNewsgroup(Guid.NewGuid(), item.ToString()));
				}
			}

			DialogResult = DialogResult.OK;

			LoadServers();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			// get all the newsgroups for this server
			clbNewsgroups.Items.Clear();
			clbNewsgroups.Enabled = true;

			Nntp nntp = new Nntp();
			Cursor = Cursors.WaitCursor;
			try
			{
				if(!cbAuthenticate.Checked)
					nntp.Connect(tbServerName.Text);
				else
					nntp.Connect(tbServerName.Text, tbUsername.Text, tbPassword.Text);

				foreach(object newsgroup in nntp.GetNewsgroups())
				{
					clbNewsgroups.Items.Add(newsgroup.ToString());
				}

				if(cbServers.SelectedItem is NntpServer)
				{
					// select items
					foreach(NntpNewsgroup ng in ((NntpServer) cbServers.SelectedItem).Newsgroups)
					{
						int index = clbNewsgroups.Items.IndexOf(ng.Name);
						if(index >= 0)
							clbNewsgroups.SetItemChecked(index, true);
					}
				}

				if(clbNewsgroups.Items.Count == 0)
				{
					clbNewsgroups.Items.Add("No Newsgroups Found");
					clbNewsgroups.Enabled = false;
				}

				nntp.Disconnect();
			}
			catch
			{
				// show error message
				clbNewsgroups.Items.Add("Error Connecting to Server");
				clbNewsgroups.Enabled = false;
			}
			Cursor = Cursors.Default;
			btnDelete.Enabled = true;
		}

		private void btnDelete_Click(object sender, System.EventArgs e)
		{
			if(cbServers.SelectedItem is NntpServer)
			{
				if(MessageBox.Show("Are you sure you want to delete this server?", "Deletion Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					m_config.Servers.Remove((NntpServer) cbServers.SelectedItem);

					DialogResult = DialogResult.OK;
					LoadServers();
				}
			}
		}
	}
}
